package tech.lq0.dreamaticvoyage.compat.jei;

import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.RecipeTypes;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IRecipeCatalystRegistration;
import mezz.jei.api.registration.IRecipeCategoryRegistration;
import mezz.jei.api.registration.IRecipeRegistration;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeManager;
import org.jetbrains.annotations.NotNull;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.block.entity.PointsStoreBlockEntity;
import tech.lq0.dreamaticvoyage.gui.screen.*;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;
import tech.lq0.dreamaticvoyage.recipe.*;

import java.util.Arrays;
import java.util.List;

@JeiPlugin
public class DmvJEIPlugin implements IModPlugin {

    @Override
    public void registerCategories(IRecipeCategoryRegistration registration) {
        registration.addRecipeCategories(new PointsStoreCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new FukamizuCompressingCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new CrystalPopperCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new CrystalPurifierCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new CrystalCutterCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new FukamizuCrushingCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new FukamizuReactingCategory(registration.getJeiHelpers().getGuiHelper()));
        registration.addRecipeCategories(new FukamizuFermenterCategory(registration.getJeiHelpers().getGuiHelper()));
    }

    @Override
    public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) {
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.POINTS_STORE.get()), PointsStoreCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.FUKAMIZU_COMPRESSOR.get()), FukamizuCompressingCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.FUKAMIZU_PUNCH.get()), FukamizuCompressingCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.CRYSTAL_POPPER.get()), CrystalPopperCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.CRYSTAL_PURIFIER.get()), CrystalPurifierCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.CRYSTAL_CUTTER.get()), CrystalCutterCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.FUKAMIZU_CRUSHER.get()), FukamizuCrushingCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.FUKAMIZU_BREAD_FURNACE.get()), RecipeTypes.BLASTING);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.FUKAMIZU_REACTION_CHAMBER.get()), FukamizuReactingCategory.TYPE);
        registration.addRecipeCatalyst(new ItemStack(ItemRegistry.FUKAMIZU_FERMENTER.get()), FukamizuFermenterCategory.TYPE);
    }

    @Override
    public void registerRecipes(IRecipeRegistration registration) {
        RecipeManager recipeManager = Minecraft.getInstance().level.getRecipeManager();

        // 积分商店配方
        registration.addRecipes(PointsStoreCategory.TYPE, Arrays.asList(PointsStoreBlockEntity.offers));

        // 深水压缩机
        registration.addRecipes(FukamizuCompressingCategory.TYPE, recipeManager.getAllRecipesFor(FukamizuCompressingRecipe.Type.INSTANCE));

        // 结晶爆米花机
        registration.addRecipes(CrystalPopperCategory.TYPE, List.of(new Object()));

        // 结晶提纯器
        registration.addRecipes(CrystalPurifierCategory.TYPE, recipeManager.getAllRecipesFor(CrystalPurifyingRecipe.Type.INSTANCE));

        // 结晶切割机
        registration.addRecipes(CrystalCutterCategory.TYPE, recipeManager.getAllRecipesFor(CrystalCuttingRecipe.Type.INSTANCE));

        // 深水粉碎机
        registration.addRecipes(FukamizuCrushingCategory.TYPE, recipeManager.getAllRecipesFor(FukamizuCrushingRecipe.Type.INSTANCE));

        // 深水反应室
        registration.addRecipes(FukamizuReactingCategory.TYPE, recipeManager.getAllRecipesFor(FukamizuReactingRecipe.Type.INSTANCE));

        // 深水发酵机
        registration.addRecipes(FukamizuFermenterCategory.TYPE, recipeManager.getAllRecipesFor(FukamizuFermentingRecipe.Type.INSTANCE));

        // 其他物品获取方式描述
        addOtherInfo(registration);
    }

    @Override
    public void registerGuiHandlers(IGuiHandlerRegistration registration) {
        registration.addRecipeClickArea(FukamizuCompressorScreen.class, 73, 43, 27, 16, FukamizuCompressingCategory.TYPE);
        registration.addRecipeClickArea(FukamizuPunchScreen.class, 73, 50, 27, 16, FukamizuCompressingCategory.TYPE);
        registration.addRecipeClickArea(CrystalPopperScreen.class, 65, 23, 18, 41, CrystalPopperCategory.TYPE);
        registration.addRecipeClickArea(CrystalPurifierScreen.class, 81, 35, 32, 16, CrystalPurifierCategory.TYPE);
        registration.addRecipeClickArea(CrystalCutterScreen.class, 75, 40, 24, 16, CrystalCutterCategory.TYPE);
        registration.addRecipeClickArea(FukamizuCrusherScreen.class, 80, 32, 11, 22, FukamizuCrushingCategory.TYPE);
        registration.addRecipeClickArea(FukamizuBreadFurnaceScreen.class, 77, 42, 22, 17, RecipeTypes.BLASTING);
        registration.addRecipeClickArea(FukamizuReactionChamberScreen.class, 103, 59, 22, 17, FukamizuReactingCategory.TYPE);
        registration.addRecipeClickArea(FukamizuFermenterScreen.class, 82, 38, 12, 29, FukamizuFermenterCategory.TYPE);
    }

    @Override
    public @NotNull ResourceLocation getPluginUid() {
        return new ResourceLocation(Utils.MOD_ID, "jei_plugin");
    }

    private void addOtherInfo(IRecipeRegistration registration) {
        registration.addItemStackInfo(new ItemStack(ItemRegistry.MUSIC_DISC_AROUND_THE_TRAVEL.get()), Component.translatable("jei.dreamaticvoyage.around_the_travel"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.MUSIC_DISC_SONG_OF_QINGQIU.get()), Component.translatable("jei.dreamaticvoyage.song_of_qingqiu"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.MUSIC_DISC_SONG_OF_AUTUMN.get()), Component.translatable("jei.dreamaticvoyage.song_of_autumn"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.ELF_WINGS.get()), Component.translatable("jei.dreamaticvoyage.elf_wings"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.PAST_SUGAR.get()), Component.translatable("jei.dreamaticvoyage.past_sugar"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.CHIRAM_CORE.get()), Component.translatable("jei.dreamaticvoyage.chiram_core"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.HAINE_CORE.get()), Component.translatable("jei.dreamaticvoyage.haine_core"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.KERORO_CORE.get()), Component.translatable("jei.dreamaticvoyage.keroro_core"));
        registration.addItemStackInfo(new ItemStack(ItemRegistry.EKIRA_CORE.get()), Component.translatable("jei.dreamaticvoyage.ekira_core"));
    }
}
